﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace eBirdAttend
{
    public partial class EnrollToBioFinger : UserControl
    {
        public string strCon = Properties.Settings.Default["ConString"].ToString();
        SqlConnection sqlCon = new SqlConnection();
        SqlCommand sqlCmd = new SqlCommand();
        SqlDataReader sqldr;
        #region Properties
        string _deviceID;

        public string DeviceID
        {
            get { return _deviceID; }
            set { _deviceID = value; }
        }
        string _deviceName;

        public string DeviceName
        {
            get { return _deviceName; }
            set { _deviceName = value; }
        }
        string _empID;

        public string EmpID
        {
            get { return _empID; }
            set { _empID = value; }
        }

        #endregion Properties

        public EnrollToBioFinger()
        {
            InitializeComponent();
            sqlCon.ConnectionString = strCon;
            sqlCmd.Connection = sqlCon;
        }      

        private void EnrollToBioFinger_Load(object sender, EventArgs e)
        {
            txtDeviceName.Text = _deviceName;
            txtDeviceId.Text = _deviceID;
        }

        private void btnEnroll_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtEmpCardId.Text.Trim() == "")
                {
                    MessageBox.Show("Please enter CardId");
                }

                else if (CheckCardIdExist())
                {
                    MessageBox.Show("Already Exist this CardId!!");
                    txtEmpCardId.Text = string.Empty;
                }
                else
                {
                    if (!EmployExists())
                    {
                        sqlCmd = new SqlCommand();
                        sqlCmd.CommandText = "insert into DevEmpDetails values('" + _empID + "','" + txtDeviceName.Text
                                             + "'," + txtDeviceId.Text + ",'"
                                             + txtEmpCardId.Text + "','"
                                             + txtEmpCardId.Text + "', null, null)";

                        sqlCmd.Connection = sqlCon;
                        sqlCon.Open();
                        sqlCmd.ExecuteNonQuery();
                        sqlCon.Close();
                        MessageBox.Show("Enrolled Succesfully");
                    }
                    else
                    {
                        sqlCmd = new SqlCommand();
                        sqlCmd.CommandText = "update DevEmpDetails set DevEmpID = '" + txtEmpCardId.Text
                            + "',CardID = '" + txtEmpCardId.Text + "' where EmpID = '" + _empID + "' and DeviceID = '" + txtDeviceId.Text + "'";
                        sqlCmd.Connection = sqlCon;
                        sqlCon.Open();
                        sqlCmd.ExecuteNonQuery();
                        sqlCon.Close();
                        MessageBox.Show("Updated Succesfully");
                    }

                    Form.ActiveForm.Visible = false;
                }
            }
            catch (Exception ex)
            {
            }
        }

        private bool CheckCardIdExist()
        {
            sqlCmd = new SqlCommand();
            sqlCmd.CommandText = "SELECT      DevEmpDetails.CardID "+
                                 " FROM         DevEmpDetails INNER JOIN EmployDetails ON "+
                                 " DevEmpDetails.EmpID = EmployDetails.EmpId "+
                                 " where DevEmpDetails.CardID='" + txtEmpCardId.Text.Trim() + "' and EmployDetails.EmpStatus=1";
            sqlCmd.Connection = sqlCon;
            sqlCon.Open();
            sqldr = sqlCmd.ExecuteReader();
            if (sqldr.Read())
            {
                sqlCon.Close();
                return true;
            }
            else
            {
                sqlCon.Close();
                return false;
            }
            
        }
        private bool EmployExists()
        {
                sqlCmd = new SqlCommand();
                sqlCmd.CommandText = "select CardID from DevEmpDetails where EmpID = '" + _empID + "' and DeviceID = '" + txtDeviceId.Text + "'";
                sqlCmd.Connection = sqlCon;
                sqlCon.Open();
                sqldr = sqlCmd.ExecuteReader();
                if (sqldr.Read())
                {
                    sqlCon.Close();
                    return true;
                }
                else
                {
                    sqlCon.Close();
                    return false;
                }
            

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
    }
}
